package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.ReqQryUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01QryUndueBillInfo extends CMB01Base implements IUndueBillInfo {
    @Override
    public RetAtsUndueBillInfo qryUndueBillInfo(ReqQryUndueBillInfo recInfo) throws Exception {
        RetAtsUndueBillInfo retAts = new RetAtsUndueBillInfo(reqHeader);
        retAts.writeStart();
        String tmpRespCode ="";
        String tmpRespInfo ="";
        try {
            CMB01Header header =  new CMB01Header();
            header.FUNNAM = "TDAFBIQY";
            XmlTextWriter xmlWriter =  new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
//            输入	TDAFBIQYX	输入属性	单记录
            xmlWriter.writeStartElement("TDAFBIQYX");
//            直联企业系统客户号	COREID	C (10)	否
            xmlWriter.writeElementString("COREID",bankFront.org);
//            经销商代码	DLENUM	C (40)	否
            xmlWriter.writeElementString("DLENUM",recInfo.DealerCode);
//            汇票号	BILLNO	C(30)	可
            xmlWriter.writeElementString("BILLNO","");
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
                        tmpRespCode ="0";
                        tmpRespInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        while (tmpBkReader.read("/CMBSDKPGK/TDAFBIQYY")){
                            RetAtsUndueBillInfoDetail detail = new RetAtsUndueBillInfoDetail();
//                            经销商代码	DLENUM	C (40)	可
                            detail.DealerCode = tmpBkReader.readedXElement.getSingleText("DLENUM");
//                            汇票号	BILLNO	C(30)	否
                            detail.BillNo = tmpBkReader.readedXElement.getSingleText("BILLNO");
//                            票面金额	DAFAMT	M	否
                            detail.BillMoney = tmpBkReader.readedXElement.getSingleText("DAFAMT");
//                            出票日期	STADAT	D	否
                            detail.BillStartDate = tmpBkReader.readedXElement.getSingleText("STADAT");
//                            票据到期日	ENDDAT	D	否
                            detail.BillEndDate = tmpBkReader.readedXElement.getSingleText("ENDDAT");
//                            开票保证金	BALAMT	M	否
                            detail.BailMoney = tmpBkReader.readedXElement.getSingleText("BALAMT");
                            retAts.writeRetDetail(detail);
                        }
                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            tmpRespCode ="-1";
            tmpRespInfo = ex.getMessage();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
